<template>
  <div>
    <!-- 头部组件 -->
    <NavBar leftText="返回"></NavBar>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <div class="goods-list">
          <div
            class="goods-item"
            v-for="(item, index) in goodsList"
            :key="index"
            @click="getGoodsDetail(item.id)"
          >
            <div class="goods_img">
              <van-image :src="item.img_url" v-lazy="item.img_url">
                <template v-slot:error></template>
              </van-image>
            </div>
            <p class="title">{{item.title}}</p>
            <div class="info">
              <p class="price">
                <span class="now">￥{{item.sell_price}}</span>
                <span class="old">￥{{item.market_price}}</span>
              </p>
              <div class="sell">
                <span>热卖中</span>
                <span>剩60件</span>
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
    <!-- 底部tab栏  -->
    <TabBar></TabBar>
  </div>
</template>

<script>
import NavBar from "../components/navBar";
import TabBar from "../components/Tabbar";
export default {
  components: {
    NavBar,
    TabBar
  },
  data() {
    return {
      // 页码
      pageindex: 1,
      // 商品数据列表
      goodsList: [],
      loading: false,
      finished: false,
      refreshing: false
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get(
        "/api/getgoods?pageindex=" + this.pageindex
      );
      if (res.status !== 0) {
        //如果状态码不为0，表示出错了
        this.$toast("获取商品数据失败，请联系管理员");
      }
      this.loading = false;
      // 拼接数组
      this.goodsList = [...this.goodsList, ...res.message];
    },
    // 监听列表加载事件
    onLoad() {
      setTimeout(() => {
        // 页码++
        this.pageindex++;
        // 加载下一页
        this.getGoodsList();
        this.loading = true;
      }, 500);
    },
    // 下拉刷新
    onRefresh() {
      //   2.重置 数据 数组
      this.goodsList = [];
      //   3.重置页码 设置为1
      this.pageindex = 1;
      //   4.重新发送请求
      this.getGoodsList();
      //   5.数据请求回来 需要手动的关闭 等待效果
      this.$toast("刷新成功");
      this.refreshing = false;
    },
    // 跳转到商品详情页
    getGoodsDetail(id) {
      // 编程式导航
      this.$router.push({ path: "/goods/detail/" + id });
    }
  }
};
</script>

<style lang="less" scoped>
.van-list {
  padding: 0;
  margin: 0;
}
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;
  padding-bottom: 80px;
  .goods-item {
    width: 47%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .goods_img {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 170px;
      padding: 0 10px;
      img {
        width: 80%;
      }
    }
    .title {
      padding: 0 10px;
      font-size: 14px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
    }

    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-size: 16px;
        }
        .old {
          font-size: 12px;
          text-decoration: line-through;
          margin-left: 10px;
        }
      }

      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
}
</style>